﻿using DotnetSpider.Core.Pipeline;
using System;
using System.Collections.Generic;
using System.Text;
using DotnetSpider.Core;
using DotnetSpider.Core.Scheduler;

using System.Linq;
using SpiderPlatform.Entity;
using SpiderPlatform.Dao;
using SpiderPlatform.Processor;
using Yamibuy.Framework.Core.Tools;
namespace SpiderPlatform.Pipe
{
    public class OscartPipe : BasePipeline
    {
        public override void Process(IEnumerable<ResultItems> resultItems, ISpider spider)
        {
            foreach (var resultItem in resultItems)
            {
                Logger.Info((resultItem.Results["ItemList"] as List<string>).Count.ToString());
                var site = new Site()
                {
                    EncodingName = "UTF-8",
                    CycleRetryTimes = 5,
                   // SleepTime = 200,
                    Headers = new Dictionary<string, string>()                {

                    { "Accept","text/html, */*; q=0.01" },
                    { "Referer", "https://www.oscart.com"},
                    { "Cache-Control","no-cache" },
                    { "Connection","keep-alive" },
                    { "Content-Type","application/x-www-form-urlencoded; charset=UTF-8" },
                    { "User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36"}
                }
                };
                foreach (var item in (resultItem.Results["ItemList"] as List<string>))
                {
                    Logger.Info(item);
                    site.AddStartUrl(item);
                }

                DotnetSpider.Core.Spider spiderDetail =
                          DotnetSpider.Core.Spider.Create(site, new QueueDuplicateRemovedScheduler(), new OscartDetailProcessor())
                               .AddPipeline(new OscartDetailPipe());                
                spiderDetail.ThreadNum = 2;
                spiderDetail.Run();
            }
        }

        public class OscartDetailPipe : BasePipeline
        {

            ItemDao dao = new ItemDao();
            List<ChannelItemEntity> itemlist = new List<ChannelItemEntity>();
            public OscartDetailPipe()
            {
                itemlist = dao.QueryChannelItem(2);
            }

            public override void Process(IEnumerable<ResultItems> resultItems, ISpider spider)
            {
                List<ChannelItemHistoryEntity> hisList = new List<ChannelItemHistoryEntity>();
                foreach (var resultItem in resultItems)
                {
                    var item = resultItem.Results["Item"] as OscartEntity;
                    Logger.Info(item.name);
                    //var content = $"{entity.id},{entity.name},{entity.price},{entity.old_price},{entity.reviews},{entity.c1},{entity.c2},{entity.link}\n";
                    //System.IO.File.AppendAllText(@"c:\oscart.csv", content,Encoding.UTF8);
                   
                   
                    hisList.Add(new ChannelItemHistoryEntity
                    {
                        channel_id = 2,
                        channel_sku = item.id,
                        score = 0,
                        is_instock = 1,
                        reviews =int.Parse(item.reviews),
                        price =decimal.Parse(item.price.TrimStart('$')),
                        title = item.name,
                        link = "https://www.oscart.com/products/" + item.id,
                        images = item.images
                      
                    });                   
                }
                dao.CreateChannelItemHistory(hisList);
                var hisExists = dao.QueryChannelItemBySku(2, hisList.Select(p => p.channel_sku).ToList());              
                //检查是否有新的商品出现
                var diffList = hisList.Except(hisExists, new ChannelItemHistoryComparer());
                //记录新的item
                var newItemList = diffList.ToList().ToTypeList<ChannelItemHistoryEntity, ChannelItemEntity>();
                if (newItemList.Count > 0)
                {
                    dao.CreateChannelItem(newItemList);
                    var realList = diffList.ToList();
                    dao.CreateChannelItemRealTime(realList);
                }
                dao.UpdateChannelItemRealTime(hisList);
            }
        }
    }
}
